package com.study_online.service.Impl;/*
    张昊（java文件）
 
*/

import com.study_online.domain.ResultInfo;
import com.study_online.domain.User;
import com.study_online.domain.Video;
import com.study_online.interceptor.LoginInterceptor;
import com.study_online.mapper.VideoMapper;
import com.study_online.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class VideoServiceImple implements VideoService {

    @Autowired
    VideoMapper videoMapper;

    //根据视频的id查询视频的地址
    @Override
    public ResultInfo findVideoId(int videoId) {
        ResultInfo info=new ResultInfo();
        info.setFlag(true);
        try {
            String videoIp = videoMapper.findVideoId(videoId);
            info.setData(videoIp);
        }catch (Exception e){
            info.setFlag(false);
            info.setErrorInfo("查询失败");
            e.printStackTrace();
        }
        return info;
    }

    //查询所有视频
    @Override
    public ResultInfo findAll() {
        ResultInfo info=new ResultInfo();
        info.setFlag(true);
        try {
            List<Video> all = videoMapper.findAll();
            info.setData(all);
        }catch (Exception e){
            info.setFlag(false);
            info.setErrorInfo("查询失败");
            e.printStackTrace();
        }
        return info;
    }

    @Override
    public ResultInfo findIntroud(Integer videoId) {
        ResultInfo info=new ResultInfo();
        info.setFlag(true);
        try {
            String introud = videoMapper.findIntroud(videoId);
            info.setData(introud);
        }catch (Exception e){
            info.setFlag(false);
            info.setErrorInfo("查询失败");
            e.printStackTrace();
        }
        return info;

    }

    @Override
    public ResultInfo findIsNote(Integer videoId) {
        ResultInfo info=new ResultInfo();
        info.setFlag(true);
        try {
            User user = LoginInterceptor.getUser();
            Integer userId = user.getUid();
            Integer isNote=videoMapper.findIsNote(videoId,userId);
            info.setData(isNote);
        }catch (Exception e){
            info.setFlag(false);
            info.setErrorInfo("查询失败");
            e.printStackTrace();
        }
        return info;
    }

    @Override
    public ResultInfo saveNote(Integer videoId) {
        ResultInfo info=new ResultInfo();
        info.setFlag(true);
        try {
            User user = LoginInterceptor.getUser();
            Integer userId = user.getUid();
            videoMapper.saveNote(videoId,userId);

        }catch (Exception e){
            info.setFlag(false);
            info.setErrorInfo("标记失败");
            e.printStackTrace();
        }
        return info;
    }

    @Override
    public ResultInfo delNote(Integer noteid) {
        ResultInfo info=new ResultInfo();
        info.setFlag(true);
        try {
            videoMapper.delNote(noteid);

        }catch (Exception e){
            info.setFlag(false);
            info.setErrorInfo("取消标记失败");
            e.printStackTrace();
        }
        return info;
    }

    /*视频数据添加的serviceImpl*/
    @Override
    public ResultInfo insertData(String videoName, String videoIntroud, int videoTypeId, String videoIp, String pictureIp, String videoPay) {
        ResultInfo info=new ResultInfo();
        info.setFlag(true);

        //添加视频的时间
        Date d = new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            boolean all = videoMapper.insertData(videoName,videoIntroud,videoTypeId,videoIp,pictureIp,videoPay,sdf.format(d));
            /*info.setData(all);*/
        }catch (Exception e){
            info.setFlag(false);
            info.setErrorInfo("视频数据插入失败");
            e.printStackTrace();
        }
        return info;
    }


}
